package com.csun.cmny.provider.device.yingshi;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.provider.device.yingshi.core.YingShiHttpClient;
import com.csun.cmny.provider.device.yingshi.entity.*;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.dto.yingshi.YingShiPlayerDto;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2021/10/12 19:03
 * @Description: TODO
 */
@Slf4j
@Component
public class YingShiUtils {

    @Resource
    private YingShiHttpClient yingShiHttpClient;

    /**
     * 获取access token
     *
     * @param appKey    the app key
     * @param secret    the secret
     *
     * @return  the access token vo.
     */
    public YingShiAccessTokenVo getAccessToken(String appKey, String secret) {

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        MultiValueMap map = new LinkedMultiValueMap();
        map.add("appKey", appKey);
        map.add("appSecret", secret);

        HttpEntity param = new HttpEntity<>(map, headers);

        YingShiVo result = yingShiHttpClient.request(YingShiConstant.GET_ACCESS_TOKEN_URL,
                HttpMethod.POST, param);

        JSONObject jsonObject= JSONObject.fromObject(result.getData());

        return (YingShiAccessTokenVo) JSONObject.toBean(jsonObject, YingShiAccessTokenVo.class);
    }

    /**
     * 获取播放地址
     *
     * @param yingShiPlayerDto
     *
     * @return
     */
    public YingShiUrlVo getPlayUrl(YingShiPlayerDto yingShiPlayerDto) {

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        MultiValueMap map = new LinkedMultiValueMap();
        // 校验AccessToken
        if (PublicUtil.isEmpty(yingShiPlayerDto.getAccessToken())) {
            throw new DmcBizException(ErrorCodeEnum.DMC100622004);
        }
        map.add("accessToken", yingShiPlayerDto.getAccessToken());
        // 校验channelNo
        if (PublicUtil.isEmpty(yingShiPlayerDto.getDeviceSerial())) {
            throw new DmcBizException(ErrorCodeEnum.DMC100622005);
        }
        map.add("deviceSerial", yingShiPlayerDto.getDeviceSerial());

        // 可选字段
        if (PublicUtil.isNotEmpty(yingShiPlayerDto.getChannelNo())) {
            map.add("channelNo", yingShiPlayerDto.getChannelNo() + "");
        }
        if (PublicUtil.isNotEmpty(yingShiPlayerDto.getCode())) {
            map.add("code", yingShiPlayerDto.getCode());
        }
        if (PublicUtil.isNotEmpty(yingShiPlayerDto.getExpireTime())) {
            map.add("expireTime", yingShiPlayerDto.getExpireTime());
        }
        if (PublicUtil.isNotEmpty(yingShiPlayerDto.getProtocol())) {
            map.add("protocol", yingShiPlayerDto.getProtocol());
        }
        if (PublicUtil.isNotEmpty(yingShiPlayerDto.getQuality())) {
            map.add("quality", yingShiPlayerDto.getQuality());
        }
        if (PublicUtil.isNotEmpty(yingShiPlayerDto.getStartTime())) {
            map.add("startTime", yingShiPlayerDto.getStartTime());
        }
        if (PublicUtil.isNotEmpty(yingShiPlayerDto.getStopTime())) {
            map.add("stopTime", yingShiPlayerDto.getStopTime());
        }
        if (PublicUtil.isNotEmpty(yingShiPlayerDto.getType())) {
            map.add("type", yingShiPlayerDto.getType());
        }
        if (PublicUtil.isNotEmpty(yingShiPlayerDto.getSupportH265())) {
            map.add("supportH265", yingShiPlayerDto.getSupportH265());
        }
        if (PublicUtil.isNotEmpty(yingShiPlayerDto.getGbchannel())) {
            map.add("gbchannel", yingShiPlayerDto.getGbchannel());
        }

        HttpEntity param = new HttpEntity<>(map, headers);

        YingShiVo result = yingShiHttpClient.request(YingShiConstant.GET_PLAY_URL,
                HttpMethod.POST, param);

        JSONObject jsonObject= JSONObject.fromObject(result.getData());

        return (YingShiUrlVo) JSONObject.toBean(jsonObject, YingShiUrlVo.class);
    }

    /**
     * 获取设备列表
     *
     * @param accessToken   用户的access token
     *
     * @return  the ying shi device vo list.
     */
    public List<YingShiDeviceVo> getDeviceList(String accessToken) {

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("accessToken", accessToken);    // 授权过程获取的access_token 必须
        map.add("pageStart", "0");    // 分页起始页，从0开始 非必须
        map.add("pageSize", "50");    // 分页大小，默认为10，最大为50 非必须

//        Map<String, Object> map = new HashMap<>();
//        map.put("accessToken", accessToken);
//        map.put("pageStart", 0);
//        map.put("pageSize", 50);

        HttpEntity param = new HttpEntity<>(map, headers);

        YingShiVo result = yingShiHttpClient.request(YingShiConstant.GET_DEVICE_LIST_URL,
                HttpMethod.POST, param);

        JSONArray jsonArray = JSONArray.fromObject(result.getData());

        return JSONArray.toList(jsonArray, YingShiDeviceVo.class);
    }

    /**
     * 获取指定设备的通道列表信息
     *
     * @param accessToken   the access token
     * @param deviceSerial  the device serial
     *
     * @return  the ying shi device camera vo list.
     */
    public List<YingShiDeviceCameraVo> getDeviceCameraList(String accessToken, String deviceSerial) {

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        MultiValueMap map = new LinkedMultiValueMap();
        map.add("accessToken", accessToken);    // 授权过程获取的access_token 必须
        map.add("deviceSerial", deviceSerial);  // 设备序列号,存在英文字母的设备序列号,字母需为大写 必须

        HttpEntity param = new HttpEntity<>(map, headers);

        YingShiVo result = yingShiHttpClient.request(YingShiConstant.GET_DEVICE_CAMERA_LIST_URL,
                HttpMethod.POST, param);

        JSONArray jsonArray = JSONArray.fromObject(result.getData());

        return JSONArray.toList(jsonArray, YingShiDeviceCameraVo.class);
    }

    /**
     * 获取单个设备信息
     *
     * @param accessToken   the access token
     * @param deviceSerial  the device serial
     *
     * @return  the ying shi device info vo
     */
    public YingShiDeviceInfoVo getDeviceInfo(String accessToken, String deviceSerial) {

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        MultiValueMap map = new LinkedMultiValueMap();
        map.add("accessToken", accessToken);
        map.add("deviceSerial", deviceSerial);

        HttpEntity param = new HttpEntity<>(map, headers);

        YingShiVo result = yingShiHttpClient.request(YingShiConstant.GET_DEVICE_INFO,
                HttpMethod.POST, param);

        JSONObject jsonObject= JSONObject.fromObject(result.getData());

        return (YingShiDeviceInfoVo) JSONObject.toBean(jsonObject, YingShiDeviceInfoVo.class);

    }
}
